﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Wpf_VirtualRouterPlus.Classes;
using Wpf_VirtualRouterPlus.UserControls;
using WPFSharp.Globalizer;
using WPFSharp.Globalizer.Converters;

namespace Wpf_VirtualRouterPlus.Windows
{
    /// <summary>
    /// Interaction logic for OptionWindow.xaml
    /// </summary>
    public partial class OptionWindow : Window, INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        protected void OnPropertyChanged(string _PropertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
                handler(this, new PropertyChangedEventArgs(_PropertyName));
        }

        HotspotSettings _manager;
        Setting _setting;
        AvailableLanguages _availableLanguages;

        public AvailableLanguages AvailableLanguages
        {
            get { return _availableLanguages; }
            set
            {
                _availableLanguages = value;
                OnPropertyChanged("AvailableLanguages");
            }
        }

        public OptionWindow(HotspotSettings manager_)
        {
            InitializeComponent();
            _manager = manager_;
            _setting = _manager.Setting;
            _initialize();
            Loaded += new RoutedEventHandler(OptionWindow_Loaded);
            Closed += new EventHandler(OptionWindow_Close);
        }

        private void _reload()
        {

        }

        private void _initialize()
        {
            // init value for combobox value
            AvailableLanguages = AvailableLanguages.getInstance();

            // load setting to window
            _loadSettings(_setting);
        }

        private void _loadSettings(Setting setting_)
        {
            String langAlias = setting_.Language;
            int index = _getIndexOfLanguage(langAlias);
            if (index >= 0)
                cbbLanguageSetting.SelectedIndex = index;
            cbStartUp.IsChecked = setting_.StartWithWindow;
        }

        private int _getIndexOfLanguage(string langAlias)
        {
            var converter = new LanguageNameConverter();
            String langName = converter.Convert(langAlias, typeof(string), null, null).ToString();

            var cbbItems = cbbLanguageSetting.Items;
            int index = 0;
            foreach (String sLang in cbbItems)
            {
                if (langName.Equals(sLang))
                {
                    return index;
                }
                index++;
            }
            return -1;
        }

        private void _checkChangedSettings()
        {
            Setting newSetting = new Setting();
            var converter = new LanguageNameConverter();
            var selectedLanguage = cbbLanguageSetting.SelectedValue;
            if (selectedLanguage != null)
                newSetting.Language = converter.ConvertBack(selectedLanguage.ToString(), typeof(string), null, null).ToString();

            newSetting.StartWithWindow = (bool)cbStartUp.IsChecked;

            if (!_setting.compare(newSetting))
            {
                String messageText = FindResource("checkChangedSettings_Text").ToString();
                String messageCaption = FindResource("checkChangedSettings_Caption").ToString();
                MessageBoxResult result = MessageBox.Show(messageText, messageCaption, MessageBoxButton.YesNo, MessageBoxImage.Warning);
                if (result == MessageBoxResult.Yes)
                {
                    _setting.setSettings(newSetting);
                    _setting.applySettings();
                    _setting.saveSettings();
                }
            }
        }

        private void OptionWindow_Loaded(object sender, RoutedEventArgs e)
        {
            _reload();
        }

        private void OptionWindow_Close(object sender, EventArgs e)
        {
            _checkChangedSettings();
            _manager.reload();
        }


        private void btnClose_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void OptionWindow_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Escape)
            {
                this.Close();
            }
        }

        private void btnApply_Click(object sender, RoutedEventArgs e)
        {
            _applySettings();
            this.Close();
        }

        private void _applySettings()
        {
            Setting newSetting = new Setting();
            var converter = new LanguageNameConverter();
            var selectedLanguage = cbbLanguageSetting.SelectedValue;
            newSetting.Language = converter.ConvertBack(selectedLanguage.ToString(), typeof(string), null, null).ToString();
            newSetting.StartWithWindow = (bool)cbStartUp.IsChecked;
            newSetting.applySettings();
            newSetting.saveSettings();
            _setting.setSettings(newSetting);
        }

        private void btnReset_Click(object sender, RoutedEventArgs e)
        {
            Setting newSetting = new Setting();
            _loadSettings(newSetting);
        }
    }
}
